//
//  CarNumberView.swift
//  koolcar
//
//  Created by xiantian on 2023/11/3.
//

import UIKit
typealias CarNumberViewBlock = (NSInteger) -> ()

class CarNumberView: UIView {
    var callBack: CarNumberViewBlock?
    ///减
    private var minBtn: UIButton = {
        let btn = UIButton()
        btn.setup_image(image: UIImage.init(named: "icon_subtract_car"))
        return btn
    }()
    
    ///加
    private var addBtn: UIButton = {
        let btn = UIButton()
        btn.setup_image(image: UIImage.init(named: "icon_add_car"))
        return btn
    }()
    
    ///数量
    private var numberBtn: UIButton = {
        let tempTF = UIButton()
        tempTF.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempTF.setup_titleColor(color: Theme.titleLabColor)
        return tempTF
    }()
    
    public var numValue: NSInteger = 0 {
        didSet {
            self.numberBtn.setup_titleLabel(text: "\(numValue)")
        }
    }
    ///最大值
    public var maxNumber: NSInteger = 9999
    
    ///最小值
    public var minNumber: NSInteger = 1
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.minBtn)
        self.addSubview(self.addBtn)
        self.addSubview(self.numberBtn)
        
        minBtn.addTarget(self, action: #selector(minBtnAction), for: .touchUpInside)
        addBtn.addTarget(self, action: #selector(addBtnAction), for: .touchUpInside)
        numberBtn.addTarget(self, action: #selector(numberBtnAction), for: .touchUpInside)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        minBtn.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.bottom.equalTo(0)
            make.top.equalTo(0)
            make.width.equalTo(30)
        }
        
        addBtn.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.width.equalTo(30)
            make.bottom.equalTo(0)
            make.right.equalTo(0)
        }
        
        numberBtn.snp.makeConstraints { make in
            make.left.equalTo(minBtn.snp.right)
            make.right.equalTo(addBtn.snp.left)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
    @objc func minBtnAction(){
        if self.numValue <= 0 {
            return
        }
        self.numValue = self.numValue - 1
        self.callBack?(self.numValue)
    }
    
    @objc func addBtnAction(){
        if self.numValue >= self.maxNumber {
            return
        }
        self.numValue = self.numValue + 1
        self.callBack?(self.numValue)
    }
    
    ///点击中间数量 弹框输入数量
    @objc func numberBtnAction(){
        let vc = ShoppingCarAlertModifyView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
        vc.titleStr = "修改购买数量"
        vc.btnTitleArray = [MMLocalizedString("cancel", comment: "取消"),
                                              MMLocalizedString("confirm", comment: "确定")]
        vc.minNumber = self.minNumber
        vc.maxNumber = self.maxNumber
        vc.number = self.numValue
        vc.setupUI()
        UIApplication.shared.keyWindow?.rootViewController?.view.addSubview(vc)

        vc.callBack = {(tagIndex, number) in
            if tagIndex == 0 {
                //取消
            }else{
                self.numValue = number
                self.callBack?(number)
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
